﻿using System;
using System.Web.Mvc;
using Less.Core.Model;

namespace LessBoard.Controllers
{
    public class ConfigController : Controller
    {
        //
        // GET: /Config/

        public ActionResult Index()
        {
            return View(new Models.ConfigModel());
        }

        //
        // GET: /Config/Details/5

        public ActionResult Details(int id)
        {
            //return View();
            return new EmptyResult();
        }

        //
        // GET: /Config/Create

        public ActionResult Create()
        {
            return View(new Models.ConfigModel().ConfigSetting);
        } 

        //
        // POST: /Config/Create 

        [HttpPost]
        public ActionResult Create(ConfigSetting record )
        {
            try
            {
                var srv = Models.ConfigModel.SRV;
                srv.Create(record);
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                Log.Error(e);
                return View();
            }
        }
        
        //
        // GET: /Config/Edit/5
 
        public ActionResult Edit(int id)
        {
            var srv = Models.ConfigModel.SRV;
            ConfigSetting confingSetting = srv.Get(id);
            return View(confingSetting);
        }

        //
        // POST: /Config/Edit/5

        [HttpPost]
        public ActionResult Edit(ConfigSetting record)
        {
            try
            {
                var srv = Models.ConfigModel.SRV;
                srv.Update(record);
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                Log.Error(e);
                return View();
            }
        }

        public ActionResult Translations()
        {
            try
            {
                return View(new Models.ConfigModel());
            }
            catch (Exception e)
            {
                Log.Error(e);
                return new EmptyResult();
            }
        }


        public ActionResult TranslationsByCode(string code)
        {
            try
            {
                var srv = Models.ConfigModel.SRV;
                var trs = srv.GetTransalationsByCode(code);
                return PartialView(trs);
            }
            catch (Exception e)
            {
                Log.Error(e);
                return new EmptyResult();
            }
        }


        public ActionResult TranslationEditor(int id)
        {
            try
            {
                var srv = Models.ConfigModel.SRV;
                var tr = srv.Get(id);
                return PartialView(tr);
            }
            catch (Exception e)
            {
                Log.Error(e);
                return new EmptyResult();
            }
        }
        [HttpPost]
        public ActionResult TranslationEditor(ConfigSetting record)
        {
            try
            {
                var srv = Models.ConfigModel.SRV;
                srv.Update(record);
                return RedirectToAction("Translations");
            }
            catch (Exception e)
            {
                Log.Error(e);
                return View();
            }
        }
    }
}
